#include<bits/stdc++.h>
using namespace std;

#define REP(i, l, r) for(int i = l; i <= r; ++i)
#define RREP(i, l, r) for(int i = l; i >= r; --i)
#define _REP(i, l, r, _) for(int i = l; i <= r; i += _)
#define _RREP(i, l, r, _) for(int i = l; i >= r; i -= _)
#define Max(a, b) a = max(a, b)

typedef long long ll;
const int N = 2e5 + 10, M = 1e6 + 10;

int T, n, a[N];
ll s[N], f[N][2], maxv[M], lst[M];

ll sum(int l, int r)
{
    return r < l ? 0ll : s[r] - s[l-1];
}

int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);
        REP(i, 1, n)
        {
            scanf("%d", a + i);
            s[i-1] = a[i] * (a[i]==a[i-1]);
            if(i-1) s[i-1] += s[i-2];
        }
        memset(f, 0, sizeof f);
        memset(maxv, -0x3f, sizeof maxv);
        memset(lst, 0, sizeof lst);
        //lst[a[1]] = 1;
        REP(i, 1, n-1)
        {
            REP(k, 0, 1) f[i][k] = max(f[i-1][0], f[i-1][1]);
            f[i][0] += sum(i,i);

            maxv[a[i+1]] += sum(lst[a[i+1]], i-1);//cout << maxv[a[i+1]] << endl;
            Max(f[i][1], maxv[a[i+1]] + a[i+1]);
            if(a[i-1] == a[i+1]) Max(f[i][1], f[i-1][1] + a[i+1]);

            Max(maxv[a[i]], f[i][1] - sum(i,i));
            lst[a[i]] = i;
        }/*
        REP(i, 1, n)
        {
            REP(k, 0, 1) cout << f[i][k] << " ";
            cout << endl;
        }*/
        printf("%lld\n", max(f[n-1][0], f[n-1][1]));
    }
    return 0;
}
